// pages/bindphone/bindphone.js
 

const host = require('../../config.js').host; 
const network = require('../../utils/network.js');
const app = getApp();
var interval = null //倒计时函数
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: "",
    hasBindPhone: false,
    userid:"",
    messCode:"",
    time: '获取验证码', //倒计时 
    currentTime: 60,
    disabled:false
  },
  getInfo() {

    let that = this;
    network.requestGet(host + '/xcx/customer/getUserInfo', {}, res => {
      console.log("个人信息：", res)
      if (res.data.code == 200) {

        if (res.data.data.phone) {
          that.setData({
            phone: res.data.data.phone,
            hasBindPhone: true,
            userid:res.data.data.id
          })
          console.log("userID="+this.data.userid)
        }


      } else {
        wx.showToast({
          title: res.data.msg ? res.data.msg : '请求失败',
          icon: "none",
          duration: 1200
        })
      }
    })
  },
 
  getPhoneNumber(e) {
    console.log(e)
    // console.log(e.detail.errMsg)
    // console.log(e.detail.iv)
    // console.log(e.detail.encryptedData)
    var that = this;
    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      that.setData({
        phone: res.result.event.weRunData.data.phoneNumber 
      })
      that.updateIphoneData(res.result.event.weRunData.data.phoneNumber)
      getApp().globalData.userInfo.phone = this.data.phone;
      setTimeout(function(){
        wx.showToast({
          title: '绑定成功',
          icon: 'none',
          duration: 1200
        })
        wx.navigateBack();
      },1500)

    }).catch(err => {
      console.error(err);
    });
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getInfo()
     let userifo=getApp().globalData.userInfo;
     if(!userifo){
        return;
     }
     console.log(userifo)
    this.setData({
      userid:userifo.id,
      phone:userifo.phone, 
    })
    if(this.data.phone){
      this.setData({
        hasBindPhone:true
      }) 
    }
  },
////////
updateIphoneData(e){
    
  let that = this;
  network.requestPost('/xcx/customer/update-customer', {
    "id":that.data.userid,
    "phone":that.data.phone
  }, res => {
    console.log("editCustomer=", res)
    if (res.data.code == 200) {
      that.setData({
        hasBindPhone: true 
      })
      wx.showToast({
        title: '绑定成功',
      })
    } else {
      wx.showToast({
        title: res.data.msg ? res.data.msg : '请求失败',
        icon: "none",
        duration: 1200
      })
    }
  })
},
 // 发送验证码
getMessageCodeData(e){
  
  let that = this;
  if(!that.data.phone){
    wx.showToast({
      title: '请填写手机号',
    })
    return;
  }
  that.getCode(); 
  that.setData({
    disabled:true
  })
   
  network.requestGet('/getSmsCode', {
    "type":0,
    "phonenumber":that.data.phone
  }, res => {
    console.log("editCustomer=", res)
    if (res.data.code == 200) { 
      wx.showToast({
        title: '验证码发送成功',
      })
    } else {
      wx.showToast({
        title: res.data.msg ? res.data.msg : '请求失败',
        icon: "none",
        duration: 1200
      })
    }
  })
},
getCode: function (options){
  var that = this;
  var currentTime = that.data.currentTime
  interval = setInterval(function () {
    currentTime--;
    that.setData({
      time: currentTime+'秒'
    })
    if (currentTime <= 0) {
      clearInterval(interval)
      that.setData({
        time: '重新发送',
        currentTime:60,
        disabled: false   
      })
    }
  }, 1000)  
},
// 绑定手机号
bindIphoneData(e){
  
  let that = this;
  if(!that.data.phone){
    wx.showToast({
      title: '请填写手机号',
    })
    return;
  }
  if(!that.data.messCode){
      wx.showToast({
        title: '请填写验证码',
      })
      return;
  }
  network.requestPost('/xcx/customer/bindPhone', {
    "code":that.data.messCode,
    "phone":that.data.phone
  }, res => {
    console.log("editCustomer=", res)
    if (res.data.code == 200) {
      that.setData({
        hasBindPhone: true 
      })
      wx.showToast({
        title: '绑定成功',
      })
      getApp().globalData.userInfo.phone = this.data.phone;
      setTimeout(function(){
        wx.showToast({
          title: '绑定成功',
          icon: 'none',
          duration: 1200
        })
        wx.navigateBack();
      },1500)

    } else {
      wx.showToast({
        title: res.data.msg ? res.data.msg : '请求失败',
        icon: "none",
        duration: 1200
      })
    }
  })
},
phoneChange(e){
  this.setData({
    phone:e.detail.value
  }) 
  
},
messCodeChange(e){
  
  this.setData({
    messCode:e.detail.value
  }) 
  
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})